package com.wynn.dao;

import com.wynn.pojo.*;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.annotations.Result;

import java.util.Date;
import java.util.List;

@Mapper
public interface PlanDao {

    //用于首页统计图
    @Select("select * from plan where productLine=#{id} and YEARWEEK(date_format(produceDate,'%Y-%m-%d')) = YEARWEEK(now())")
    @Results({
            @Result(column = "productLine",property = "productLine"
                    ,one = @One(select = "com.wynn.dao.ProductLineDao.findProductLineById")),
            @Result(column = "product",property = "product"
                    ,one = @One(select = "com.wynn.dao.ProductDao.findProductById"))
    })
    List<Plan> findPlanByLineWeekly(int id);

    //整合生产计划
    @Select("select distinct * from plan Group by produceDate,productLine")
    @Results({
            @Result(column = "productLine",property = "productLine"
            ,one = @One(select = "com.wynn.dao.ProductLineDao.findProductLineById"))
    })
    List<PlanSummary> findSummary();

    @Select("select number from plan where produceDate=#{produceDate} AND productLine=#{productLine.id} AND product=#{product.id}")
    Integer findProductNum(@Param("produceDate") Date produceDate,@Param("productLine") ProductLine productLine,@Param("product") Product product);

    @Insert("insert into plan values(null,#{produceDate},#{productLine},#{id},#{num})")
    void addPlan(@Param("produceDate")Date produceDate, @Param("productLine")int productLine, @Param("id")int id, @Param("num")Integer num);


    @Update("update plan set number=#{value} where produceDate=#{produceDate} AND productLine=#{productLine.id} and product=#{pid}")
    void editPlan(@Param("produceDate") Date produceDate,@Param("productLine") ProductLine productLine, @Param("pid")int id,@Param("value") Integer value);

    @Delete("delete from plan where productLine=#{productLine.id} AND produceDate=#{date}" )
    void deletePlan(@Param("date") Date produceDate, @Param("productLine") ProductLine productLine);

    //用于导入生产计划
    @Delete("delete from plan where MONTH(produceDate) = MONTH(#{produceDate})")
    void deletePlanByMonth(Date produceDate);

    //按月查询
    @Select("<script>" +
                "select distinct * from plan " +
            "<where> " +
                "<if test='productLine.id!=0'>and productLine = #{productLine.id} </if>" +
                "<if test='produceDate!=null'> and MONTH(produceDate) = MONTH(#{produceDate})+1 </if>" +
            "</where>" +
                "Group by produceDate,productLine" +
            "</script>")
    @Results({
            @Result(column = "productLine",property = "productLine"
                    ,one = @One(select = "com.wynn.dao.ProductLineDao.findProductLineById"))
    })
    List<PlanSummary> searchPlan(@Param("productLine") ProductLine productLine, @Param("produceDate")Date produceDate);

    //按日查询
    @Select("<script>" +
            "select distinct * from plan " +
            "<where> " +
            "<if test='productLine.id!=0'>and productLine = #{productLine.id} </if>" +
            "<if test='produceDate!=null'> and to_days(produceDate) = to_days(#{produceDate})</if>" +
            "</where>" +
            "Group by produceDate,productLine" +
            "</script>")
    @Results({
            @Result(column = "productLine",property = "productLine"
                    ,one = @One(select = "com.wynn.dao.ProductLineDao.findProductLineById"))
    })
    PlanSummary searchOnePlan(@Param("productLine") ProductLine productLine, @Param("produceDate")Date produceDate);

    @Select("select * from schedule where to_days(produceDate)=to_days(#{produceDate}) AND productLine=#{productLine.id}")
    @Results({
            @Result(column = "order1",property = "product1"
                    ),
            @Result(column = "order2",property = "product2"
            ),
            @Result(column = "order3",property = "product3"
                    ),
            @Result(column = "order4",property = "product4"
                    ),
            @Result(column = "order5",property = "product5"
                    ),
            @Result(column = "order6",property = "product6"
                    ),
            @Result(column = "order7",property = "product7"
                    ),
            @Result(column = "order8",property = "product8"
                    ),
            @Result(column = "order9",property = "product9"
                    ),
            @Result(column = "order10",property = "product10"
                    ),
            @Result(column = "order11",property = "product11"
                    ),
            @Result(column = "order12",property = "product12"
                    ),
            @Result(column = "order13",property = "product13"
                    ),
            @Result(column = "order14",property = "product14"
                    ),
            @Result(column = "order15",property = "product15"
                    ),
            @Result(column = "order16",property = "product16"
                    ),
            @Result(column = "order17",property = "product17"
                    ),
            @Result(column = "order18",property = "product18"
                    ),
            @Result(column = "order19",property = "product19"
                    ),
            @Result(column = "order20",property = "product20"
                    ),
            @Result(column = "order21",property = "product21"
                    ),
            @Result(column = "order22",property = "product22"
                    ),
            @Result(column = "order23",property = "product23"
                    ),
            @Result(column = "order24",property = "product24"
                    ),
            @Result(column = "order25",property = "product25"
                    ),
            @Result(column = "order26",property = "product26"
                    ),
            @Result(column = "order27",property = "product27"
                    ),
            @Result(column = "order28",property = "product28"
                    ),
            @Result(column = "order29",property = "product29"
                    ),
            @Result(column = "order30",property = "product30"
                    ),
            @Result(column = "order31",property = "product31"
                    ),
            @Result(column = "order32",property = "product32"
                    ),
    })
    Schedule findSchedule(@Param("produceDate") Date produceDate,@Param("productLine") ProductLine productLine);

    @Insert("insert into schedule values(null,#{product1},#{product2},#{product3},#{product4},#{product5},#{product6},#{product7},#{product8},#{product9},#{product10},#{product11},#{product12},#{product13},#{product14},#{product15},#{product16},#{product17},#{product18},#{product19},#{product20},#{product21},#{product22},#{product23},#{product24},#{product25},#{product26},#{product27},#{product28},#{product29},#{product30},#{product31},#{product32},#{produceDate},#{productLine.id})")
    void addSchedule(Schedule schdule);

    @Select("select * from calcuStatue where produceDate=#{produceDate} and productLine=#{id}")
    CalcuStatue calcuStatue(@Param("produceDate") Date produceDate,@Param("id") int id);

    @Insert("insert into calcustatue values(#{produceDate},#{productLine},#{id})")
    void addCalcuStatue(@Param("productLine") int productLine,@Param("produceDate") Date produceDate,@Param("id") int i);

    @Delete("delete from calcustatue where productLine=#{productLine} and to_days(produceDate)=to_days(#{produceDate})")
    void deleteCalcuStatue(@Param("productLine")int productLine,@Param("produceDate") Date produceDate);
}
